#!/usr/bin/python
# -*- coding: utf-8 -*-

'''
Introduction to Programming in Java - An Interdisciplinary Approach

All book programs ported to Python by
Scott C. King (revisionx at {gee}ma1l <dot> c0m).

All program examples pass Pylint with a perfect 10.00 score.
Pylint:  http://www.pylint.org/


1.4:  Array Operations
An assortment of code snippets from this section.


Information on Python's copy module:
https://docs.python.org/2/library/copy.html

Information on Python's random module:
https://docs.python.org/2/library/random.html

'''

import random
import copy


def main():
    ''' Typical uses of array-processing code '''

    # create array with random values
    new_array = []
    for _ in range(10):
        new_array.append(random.random())


    # print the array values, one per line
    for _ in new_array:
        print _


    # print the maximum of the array values
    print 'Max: ', max(new_array)


    # compute the average of the array values
    total = 0
    for i in new_array:
        total += i
    print '\nAverage: ', total / len(new_array)


    # copy to another array (shallow)
    another_array = copy.copy(new_array)
    for _ in another_array:
        print _
    print '\nAre Arrays 1 and 2 Equal? ', new_array == another_array
    print 'Are Arrays 1 and 2 The Same? ', new_array is another_array


    # reverse the elements within an array (manually)
    print '\nFirst Reverse (Manually)'
    for index in range(len(another_array) / 2):
        temp = another_array[index]
        another_array[index] = another_array[len(another_array) -1 - index]
        another_array[len(another_array) -1 - index] = temp

    for _ in another_array:
        print _


    # reverse the elements using Python's reverse...
    print '\nSecond Reverse using .reverse()'
    another_array.reverse()
    for _ in another_array:
        print _
    print '\nAre Arrays 1 and 2 Equal? ', new_array == another_array
    print 'Are Arrays 1 and 2 The Same? ', new_array is another_array
    print


def card_arrays():
    ''' Book array examples using playing cards '''

    # Setting array values
    suits = ['Clubs', 'Diamonds', 'Hearts', 'Spades']
    ranks = ['2', '3', '4', '5', '6', '7', '8', '9', '10', 'Jack',
             'Queen', 'King', 'Ace']
    for _ in range(5):
        print random.choice(ranks) + ' of ' + random.choice(suits)


    # setting array values at runtime (compute the array)
    deck = []
    for suit in suits:
        for rank in ranks:
            deck.append(rank + ' of ' + suit)

    print deck


    # 'manually' shuffle the deck (python has a random.shuffle() function)
    cards_in_deck = len(deck)
    print '\nCards in Deck: ', cards_in_deck
    print
    for card in range(cards_in_deck):
        card_index = card + int((random.random() * (cards_in_deck - card)))
        swap = deck[card]
        deck[card] = deck[card_index]
        deck[card_index] = swap

    print deck
    cards_in_deck = len(deck)
    print '\nCards in Shuffled Deck: ', cards_in_deck


def nested_arrays():
    ''' double[][] a = new double[M][N] initialized in Python '''

    print
    # Java-style init manual creation
    my_array = []
    for _ in range(8):
        my_array.append('0')
    my_array = [my_array] * 8
    for array in my_array:
        print array

    print
    # Java-style init using a nested list comprehension
    my_array = [['0' for _ in range(8)] for _ in range(8)]
    for array in my_array:
        print array

    # Exercise 1.3.27 as a two-dimensional array
    print
    print 'Chessboard'
    for col in range(8):
        for row in range(8):
            if row % 2 == 0:
                if col % 2 == 0:
                    my_array[col][row] = 'W'
                else:
                    my_array[col][row] = 'B'
            else:
                if col % 2 == 0:
                    my_array[col][row] = 'B'
                else:
                    my_array[col][row] = 'W'

    for array in my_array:
        print array


def rowcol_averages():
    ''' Compute row and column averages for given 11x4 array '''

    grades = [[99, 85, 98, 0], [98, 57, 78, 0], [92, 77, 76, 0],
              [94, 32, 11, 0], [99, 34, 22, 0], [90, 46, 54, 0],
              [76, 59, 88, 0], [92, 66, 89, 0], [97, 71, 24, 0],
              [89, 29, 38, 0], [0, 0, 0, 0]]
    print '\nGrades array: '
    for _ in grades:
        print _


    # compute row averages
    print '\nRows averaged:'

    row = None
    col = None
    for row in range(11):
        total = 0.0
        for col in range(4):
            total += grades[row][col]
        grades[row][col] = round(total/col)
    for _ in grades:
        print _

    # compute column averages
    print '\nColumns averaged:'
    for col in range(4):
        total = 0.0
        for row in range(11):
            total += grades[row][col]
        grades[row][col] = round(total/row)
    for _ in grades:
        print _


if __name__ == '__main__':
    main()
    card_arrays()
    nested_arrays()
    rowcol_averages()
